package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix3D;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.utils.setInterval;
	
	import popple.cameras.Camera;
	import popple.display.DisplayObject;
	import popple.display.FormatFrame;
	import popple.display.Image;
	import popple.display.MovieClip;
	import popple.display.Sprite;
	import popple.display.Stage;
	import popple.display.TBitmap;
	import popple.display.text.TextField;
	
	public class SimpleTest extends Stage
	{
		[Embed(source="background.png")]
		private var img:Class;
		[Embed(source="fighter.png")]
		private var element:Class;
		
		private var bd:TBitmap;
		private var fighter:BitmapData;
		private var texts:Vector.<TextField>;
		private var frameInfo:Vector.<TBitmap>;
		
		private var objs:Array=[];
		private var m:Camera;
		private var mtx:Matrix3D=new Matrix3D;
		public function SimpleTest()
		{
			m=new Camera
				m.active=true;
		   //bd=new TBitmap((new img as Bitmap).bitmapData);
			fighter=(new  element as Bitmap).bitmapData;
			var ff:FormatFrame=new FormatFrame(fighter,16,1,1,11);
			var bs:TBitmap=new TBitmap(new BitmapData(32,64,true,0xff9900cc));
			setInterval(changeCamera,5000);
			frameInfo=ff.data;
			
			this.enableSort=true;
			this.sortBy(sortFun);
			
			var mm:uint=40;
			var hh:uint=1;
			var zz:uint=40;
			for(var i:uint=0;i<mm;i++)
			{
				for(var j:uint=0;j<hh;j++)
				{
					for(var k:uint=0;k<zz;k++)
					{
						var mc:MovieClip=new MovieClip(frameInfo,36);
					
						
						
						/*var ft:TextFormat=new TextFormat;
						ft.color=0x9900cc;
						ft.size=14;
						var tf:TextField=new TextField();
						tf.width=100;
						tf.text="恐龙战士";
						tf.multiline=true;
						tf.wordWarp=true;
						tf.x=25;
						tf.y=-10
						tf.setTextFormat(ft);
						
						mc.addChild(tf)*/
						/*var cc:Image=new Image(bs);
						mc.addChild(cc);*/
					
						
					mc.x=i*(1280/mm);
					mc.y=j*128+600;
					mc.z=k*10;
					mc.data=Math.random()*3.0;
					mc.addEventHandle("enterFrame",onEnter);
					addChild(mc);
					objs.push(mc)
					}
					
				}
			}
		 
		}
		private function changeCamera():void
		{
			var rnd:uint=Math.random()*objs.length;
			mtx.identity();
			mtx.appendTranslation(0,-.2,0);
			m.watch(objs[rnd],mtx);
		}
		private function onEnter(e:DisplayObject):void
		{
			e.z+=e.data;
			if(e.z>1000){e.data=-e.data}else if(e.z<-100){e.data=-e.data};
		}
		private function sortFun(prev:DisplayObject,next:DisplayObject):int
		{
			var first:Number=prev.z;
			var last:Number=next.z;
			var result:Number=last-first;
			if(result>0)
			{
				return 1;
			}
			else if(result<0)
			{
				return -1
			}
			return 0;
		}
	}
}